#include "postdata.h"

// Onenet的APIKEY
String APIKEY = "DCDN=HCYV84=u218lrdrGWvcuA4=";
// Onenet的域名
String HOST = "api.heclouds.com";
//若无DNS，使用Server服务端的IP 183.230.40.33
const char* host = "183.230.40.33";
String url = "http://api.heclouds.com/devices/611697416/datapoints?type=3";
const char OneNetServer[] = "api.heclouds.com";

// 通过HTTPClient类发送POST请求
void HTTPClient_POST(char tem[5]) {
    WiFiClient client;
    HTTPClient http;
    DHT dht(2, DHT11);
    dht.begin();
    
    const uint16_t port = 80;
    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        delay(5000);
        return;
    } else {
        Serial.println("WiFiClient connect ok");
        if(http.begin(client, url)) {
            Serial.print("Remote Server Connect OK!");
            Serial.printf("[HTTP} connect ok\n");
            http.addHeader("api-key", APIKEY);
            http.addHeader("Host", HOST);
            http.addHeader("Content-Length", "10");
            http.addHeader("Connection", "close");


            float Humidity = dht.readHumidity();
            float temperature = dht.readTemperature();
            String postdata = "{\"" + String("Humidity") + "\":" + String(Humidity) + ",\"" + String("Temperature") + "\":" + String(temperature) + "}" ;

            Serial.print("Message that you send-->");
            Serial.println(postdata);

            int httpCode = http.POST(postdata);

            if (httpCode > 0) {
                // 如果状态码正常
                if (httpCode == HTTP_CODE_OK) {
                    Serial.print("HTTP状态码->");
                    Serial.println(httpCode);
                    String payload = http.getString();
                    Serial.print("Message that Server return -->");
                    Serial.println(payload);

                }
            }
            else {
                Serial.println("HTTP code < 0");
                Serial.println(httpCode);
            }
            http.end();
        } else {
            Serial.printf("[HTTP} Unable to connect\n");
        }

    }

}

//WiFiClient创建一个TCP客户端，可以连接到client.connect（）中定义的指定Internet IP地址服务器的端口。
//用HTTP协议连接OneNET服务器，并用POST方法上报数据
void WiFiClient_POST(char tem[5]) {

    const int httpPort = 80;                //Server服务端的端口号
    // 用HTTP协议连接OneNET服务器
    WiFiClient client;
    DHT dht(2, DHT11);
    dht.begin();
    //用WiFiClient类创建 client 对象，实现HTTP协议连接OneNET服务器
    if (!client.connect(host, httpPort)) {       //若连接 Onenet服务器 失败就等5秒，重新连接
        Serial.println("WiFiClient connection failed");
        delay(5000);                            //等5秒
        return;                                //程序返回到loop重新开始
    }

    if (client.connected()) {              //若连接成功，就发送POST 请求上报数据
        Serial.println("connection succ");
        
        float temperature = dht.readTemperature();
        float Humidity = dht.readHumidity();
        String data = "{\"" + String("Humidity") + "\":" + String(Humidity) + ",\"" + String("Temperature") + "\":" + String(temperature) + "}" ;
        Serial.println(data);
        // 创建发送指令 -- We now combine the request to the server
        String post_data = "POST " + url + " HTTP/1.1\r\n" +
                           "api-key:" + APIKEY + "\r\n" +
                           "Host:" + OneNetServer + "\r\n" +
                           "Content-Length: " + String(data.length()) + "\r\n" +                     //发送数据长度
                           "Connection: close\r\n\r\n" +
                           data;
        Serial.println(post_data);
        // 发送指令 -- This will send the request to server
        client.print(post_data);


        Serial.println("send succ");
    }
    // 关闭HTTP客户端
    client.stop();
    delay(5000);
}
